/**
 * 
 */
package com.et114.modules.products.securitydoor.services;

import java.util.List;

import com.et114.components.simpletable.DataList;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.core.utility.FileUploadUtil;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.Context;
import com.et114.modules.products.search.SearchService;
import com.et114.modules.products.search.SearchVo;
import com.et114.modules.products.securitydoor.dao.SecuritySystemDao;
import com.et114.modules.products.securitydoor.vo.SecurityDoor;

/*
 * @author guanhw 
 */
public class SecuritySystemServiceImpl extends BaseServicesImpl {
	private SecuritySystemDao securitySystemDao ;
	private SearchService   searchCompass ; 

	public void setSecuritySystemDao ( SecuritySystemDao securitySystemDao ) {
		this.securitySystemDao = securitySystemDao;
	}
	
	public void setSearchCompass ( SearchService searchCompass ) {
		this.searchCompass = searchCompass;
	}

	public DataList getSecuritySystems( SecurityDoor vo ) throws Exception {
		return securitySystemDao.getSecuritySystems ( vo ) ; 
	}
	
	public List< SecurityDoor > getSecuritySystemsList( SecurityDoor vo ) throws Exception {
		return securitySystemDao.getSecuritySystemsList ( vo ) ; 
	}	
	
	public List< SecurityDoor  > getSecuritySystemsHasOffTime( SecurityDoor vo ) throws Exception {
		return securitySystemDao.getSecuritySystemsHasOffTime( vo ) ; 
	}
	
	public Long saveSecuritySystems( SecurityDoor vo ) throws Exception { 
		return securitySystemDao.saveSecuritySystems ( vo ) ; 
	}
	
	public void updateSecuritySystems( SecurityDoor vo ) throws Exception { 
		securitySystemDao.updateSecuritySystems ( vo ) ; 
		
		//
		SecurityDoor cp = getSecuritySystem( vo.getProId ( ) ) ; 
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getSecuritySystem4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_SS  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_SS ) ; 				
		}		
	}
	
	
	public void updateSecuritySystemProOnOffStatus ( SecurityDoor cp )
			throws Exception {
		securitySystemDao.updateSecuritySystemProOnOffStatus ( cp );

		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getSecuritySystem4Compass ( cp.getProId ( ) );
				if ( svo != null )
					searchCompass.setPro ( cp.getProId ( ) , Const.PRO_TYPE_SS ,
							svo );
			} else
				searchCompass.removePro ( cp.getProId ( ) , Const.PRO_TYPE_SS );
		}
	}
	
	public SecurityDoor getSecuritySystem( Long id  ) throws Exception {  
		return securitySystemDao.getSecuritySystem ( id ) ; 
	}	
	
	public void removeSecuritySystem( Long id ) throws Exception {
		//delete compass
		SearchVo svo = getSecuritySystem4Compass ( id ) ; 
		if ( svo != null )
			searchCompass.removePro  ( id , Const.PRO_TYPE_SS ) ; 
		
		
		SecurityDoor ss = getSecuritySystem(  id) ; 
		if ( ss != null  ) {
			FileUploadUtil.removeFile ( ss.getSsImage ( ) ) ; 
			securitySystemDao.removeSecuritySystem ( id ) ;
		}
		Context.removeProContent ( id , Const.PRO_TYPE_SS ) ; 
	}
	
	//4 q 
	public DataList getSecuritySystems4View( SecurityDoor vo ) throws Exception {
		return securitySystemDao.getSecuritySystems4View ( vo ) ; 
	}
	
	
	public SecurityDoor getBaseSecuritySystemById4View ( Long id )
			throws Exception {
		return getBaseSecuritySystemById4View ( id , Const.PRO_PUB_STATUS_PUBED );
	}
	
	public SecurityDoor getBaseSecuritySystemById4View ( Long id ,
			Byte proOnOffStatus ) throws Exception {
		SecurityDoor vo = new SecurityDoor ( );
		vo.setProId ( id );
		vo.setProOnOffStatus ( proOnOffStatus );
		return securitySystemDao.getBaseSecuritySystemById4View ( vo );
	}
	
	public SearchVo getSecuritySystem4Compass( Long proId ) {
		return securitySystemDao.getSecuritySystem4Compass ( proId ) ; 
	}
}
